<?php
/**
 * "YFlowPlayer"
 *
 * flowpalyer site url: <a href="http://flowplayer.org">Flow Player</a>
 *
 * @author hiscaler <hiscaler@gmail.com>
 * @link http://www.yuandot.com/
 * @copyright Copyright &copy; 2009-2010 Yuandot Information Technology CO.,LTD.
 * @license http://doit.yuandot.com/license/
 */

/**
 * 如果传入的是视频文件，则调用对应的播放器进行播放，如果是链接文件，则直接将链接地址
 * 写在页面中。
 */

class YFlowPlayer extends CWidget {
    private $baseUrl;
    public $video;
    public $htmlOptions = array();

    /**
     * Initialize the widget
     */
    public function init() {
        parent::init();
        $this->htmlOptions['style'] = 'display:block; width: 100%; height: 400px;';
    }


    /**
     * Publishes the assets
     */
    public function publishAssets() {
        $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
        $this->baseUrl = Yii::app()->getAssetManager()->publish($dir);
    }

    /**
     * Registers the external javascript files
     */
    public function registerClientScripts() {
    // add the script
        $cs = Yii::app()->getClientScript();
        $cs->registerCoreScript('jquery');
        $baseDir = dirname(__FILE__);
        $assets = Yii::app()->getAssetManager()->publish($baseDir.DIRECTORY_SEPARATOR . 'assets');
        $cs->registerScriptFile($assets . '/flowplayer-3.1.4.min.js');
    }

    /**
     * Run the widget
     */
    public function run() {
        $this->publishAssets();
        $this->registerClientScripts();

        $output = 'Error';
        $v = explode('.', $this->video);
        if (count($v) != 2) { // 引用的视频路径
            $output = $this->video;
        } else {
            $id = time();
            $this->htmlOptions['id'] = $id;
            $this->htmlOptions['href'] = $this->video;
            $output = CHtml::openTag('a', $this->htmlOptions);
            $output .= CHtml::closeTag('a');
            $output .= "<script type=\"text/javascript\">flowplayer(\"{$id}\", \"{$this->baseUrl}/flowplayer-3.1.5.swf\");</script>";
        }
        
        parent::run();
        echo $output;
    }

}